<?php

class ItemLocationsController extends BaseController {

	public function __construct()
    {
        $this->beforeFilter('isAdminGet', array('on' => 'get'));
        $this->beforeFilter('isAdminPost', array('on' => 'post'));
    }
	/**
	 * Display a listing of the resource.
	 * GET /locations
	 *
	 * @return Response
	 */
	public function index()
	{
		//
	}

	public function showLocations()
	{
		//$locations = DB::table('item_locations')->get();
		$locations = ItemLocation::all();
		return View::make('locations.locationsHome')->with('locations', $locations);
	}

	public function getLocationInfo()
	{
		$error = false;
		$message = "";
		$html;
		$response;
		try
		{
			$location = ItemLocation::find(Input::get('pk'));
			if($location == null)
			{
				$error = true;
				$message = "Error: no se encontró el lugar.";
			}
			else
			{
				$html = View::make('locations.moreInfoPopup')->with('location', $location)->render();
			}
			
		}
		catch(Exception $e)
		{
			$error = true;
			$message = "Error: ".$e->getMessage();
		}
		if($error)
		{
			$response = array("error" => $error, "message" => $message);
			
		}
		else
		{
			$response = array("error" => $error, "html" => $html);
		}
		return json_encode($response);
	}

	public function getLocationItems()
	{
		$error = false;
		$message = "";
		$html;
		$response;
		try
		{
			$location = ItemLocation::find(Input::get('pk'));
			if($location == null)
			{
				$error = true;
				$message = "Error: no se encontró el lugar.";
			}
			else
			{
				$html = View::make('locations.itemsInLocationPopup')->with('items', $location->items)->render();
			}
			
		}
		catch(Exception $e)
		{
			$error = true;
			$message = "Error: ".$e->getMessage();
		}
		if($error)
		{
			$response = array("error" => $error, "message" => $message);
			
		}
		else
		{
			$response = array("error" => $error, "html" => $html);
		}
		return json_encode($response);
	}

	public function getLocationEdit()
	{
		$error = false;
		$message = "";
		$html;
		$response;
		try
		{
			$location = ItemLocation::find(Input::get('pk'));
			if($location == null)
			{
				$error = true;
				$message = "Error: no se encontró el lugar.";
			}
			else
			{
				$html = View::make('locations.editPopup')->with('location', $location)->render();
			}
			
		}
		catch(Exception $e)
		{
			$error = true;
			$message = "Error: ".$e->getMessage();
		}
		if($error)
		{
			$response = array("error" => $error, "message" => $message);
			
		}
		else
		{
			$response = array("error" => $error, "html" => $html, "location"=>$location->toJson());
		}
		return json_encode($response);
	}

	public function getLocationCreate()
	{
		$error = false;
		$message = "";
		$html;
		$response;
		try
		{
			$html = View::make('locations.createPopup')->render();
			
		}
		catch(Exception $e)
		{
			$error = true;
			$message = "Error: ".$e->getMessage();
		}
		if($error)
		{
			$response = array("error" => $error, "message" => $message);
			
		}
		else
		{
			$location = new ItemLocation;
			$location->location_type_fk = 2;
			$response = array("error" => $error, "html" => $html, "location"=>$location->toJson());
		}
		return json_encode($response);
	}

	public function saveLocation()
	{
		$error = false;
		$message = "";
		$html;
		$response;
		$locationArray = Input::get('location');
		
		try
		{
			$location = null;
			if(isset($locationArray['pk']))
			{
				$location = ItemLocation::find($locationArray['pk']);
			}
			
			$isUpdate = isset($locationArray['pk']) && $location != null;
			if(!$isUpdate)
			{
				$location = new ItemLocation;
			}

			$validator = Validator::make(
			    array(
			    	'description'=> $locationArray['description']
			    ),
			    array(
			    	'description'=> 'required|min:1|max:249'
			    )
			);
			
			if($validator->fails())
			{
				$ms = "";
				foreach ($validator->messages()->all('-:message') as $message)
				{
				    $ms .= $message;
				}
				throw new Exception($ms);
				
			}

			$location->description = $locationArray['description'];
			$location->save();
		}
		catch(Exception $e)
		{
			$error = true;
			$message = "Error: ".$e->getMessage();
		}
		if($error)
		{
			$response = array("error" => $error, "message" => $message);
			
		}
		else
		{
			$response = array("error" => $error);
		}
		return json_encode($response);
	}

	public function deleteLocation()
	{
		$error = false;
		$message = "";
		$response;
		try
		{
			$location = ItemLocation::find(Input::get('location'));

			if($location != null)
			{
				$items = $location->items;
				if($items == null || $items->isEmpty())
				{
					$location->delete();
				}
				else
				{
					throw new Exception("La localización tiene ".$location->items()->count()." items asociados.");
					
				}
				
			}
			
		}
		catch(Exception $e)
		{
			$error = true;
			$message = "Error: ".$e->getMessage();
		}
		if($error)
		{
			$response = array("error" => $error, "message" => $message);
			
		}
		else
		{
			$response = array("error" => $error);
		}
		return json_encode($response);
	}
}